@<template>
  <div class="header-box">
    <!-- 头部样式结构 -->
    <div class="top flex-four">
      <div class="top-left">
        <i class="el-icon-s-unfold" @click="expansion" v-if="isCollapse"></i>
        <i class="el-icon-s-fold" @click="expansion" v-else></i>
        <span>首页</span>
      </div>
      <div class="top-right flex-four">
        <img src="@/assets/logo.png" alt="" />
        <!-- 下拉选择  ui-->
        <div>
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="el-dropdown-link">
              用户名称<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <!--  -->
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <!-- 路由记录:
      1.vuex 路由导航js ui -->
    <div class="bottom">
      <el-tag
        :key="tag.title"
        v-for="(tag,index) in routerDatas"
        :closable="routerDatas.length > 1 ? true:false"
        :disable-transitions="false"
        @close="handleClose(tag,index)"
        @click="goRouter(tag)"
        :effect="tag.show ? 'dark' : 'light'"
      >
        {{ tag.title }}
      </el-tag>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // dynamicTags: ['标签一', '标签二', '标签三'],
    };
  },
  computed: {
    isCollapse: function () {
      //console.log(this.$store.state.expansion)  //获取到vuex state中的数据
      return this.$store.state.expansion;
    },
    //获取vuex中state的数据
    routerDatas:function(){
      //console.log(this.$store.state.routerData)
       return this.$store.state.routerData;
    }
  },
  methods: {
    expansion() {
      //要么是展开，或者是收缩 true,把vuex中的数据expansion修改
      if (this.$store.state.expansion) {
        this.$store.commit("changEx", false);
      } else {
        this.$store.commit("changEx", true);
      }
    },
    //删除路由
      handleClose(tag,index) {
        //this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
        // console.log(index)
        this.$store.commit('deData',index)
        //获取路由记录数据
        // console.log(this.routerDatas)
        if(tag.show){
          let path = this.routerDatas[this.routerDatas.length-1]
          // console.log(path)
          this.$router.push(path.path)
        }
      },
      //切换路由
      goRouter(tag){
          // console.log(tag)
          this.$router.push(tag.path)
          this.$store.commit('changeActive',tag.path)
      },
      //退出登录
      handleCommand(){
        //清除本地存储   sessionStorage
        sessionStorage.clear()
        //回到外部页面
        this.$router.replace('/') //replace() 浏览器不会有记录
      }
  },
};
</script>

<style scoped lang='less'>
//
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
//
.el-tag + .el-tag {
    margin-left: 10px;
  }
  //
.header-box {
  padding: 12px 25px;
  // border-bottom:1px solid rgba(56, 55, 55,.2);
  box-shadow: 0 1px 4px rgba(0, 21, 41, .08);
  border-radius: 20px;
  .top-left {
    font-size: 14px;
    span {
      margin-left: 8px;
      color: #606022;
    }
    i {
      cursor: pointer;
    }
  }
  .top-right {
    img {
      height: 30px;
      width: 30px;
      border-radius: 50%;
      margin-right: 10px;
    }
  }
  .bottom{
    padding: 8px 0;
    margin-top: 10px;
    border-top: 1px solid rgba(56, 55, 55,.2);
  }
}
</style>